package spring.cloud.eurekaclientproduct.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.spring.cloud.commonutils.vo.UserInfo;
import com.spring.cloud.commonutils.vo.ResultMessage;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/test")
    public Map<String,String> test(){
        Map<String, String> map = new HashMap<>();
        map.put("test","test");
        return map;
    }

    @GetMapping("/purchase/{userId}/{productId}/{amount}")
    public ResultMessage purchaseProduct(
            @PathVariable("userId") Long userId,
            @PathVariable("productId") Long productId,
            @PathVariable("amount") Double amount
    ){
        System.out.println("扣减产品余额.");
        String url = "http://EUREKA-CLIENT-FUND/fund/account/balance/{userId}/{amount}";
        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        params.put("amount",amount);
        ResultMessage rm = restTemplate.postForObject(url, null, ResultMessage.class, params);
        System.out.println(rm.getMessage());
        System.out.println("记录交易信息");
        return new ResultMessage(true,"交易成功");
    }
}
